<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - 校园二手交易平台</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }
        .register-container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: center;
        }
        .register-container h2 {
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
            text-align: left;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn-register {
            width: 100%;
            padding: 10px;
            background-color: #5c67f2;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }
        .btn-register:hover {
            background-color: #5058e0;
        }
        .link-login {
            margin-top: 20px;
            font-size: 14px;
        }
        .link-login a {
            color: #5c67f2;
            text-decoration: none;
        }
        .link-login a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <h2>用户注册</h2>
        <form id="registrationForm">
            <div class="form-group">
                <label for="studentId">学号:</label>
                <input type="text" id="studentId" name="student_id" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
             <div class="form-group">
                <label for="confirmPassword">确认密码:</label>
                <input type="password" id="confirmPassword" required>
            </div>
            <div class="form-group">
                <label for="realName">真实姓名:</label>
                <input type="text" id="realName" name="real_name" required>
            </div>
            <div class="form-group">
                <label for="nickname">昵称:</label>
                <input type="text" id="nickname" name="nickname">
            </div>
            <div class="form-group">
                <label for="email">邮箱:</label>
                <input type="email" id="email" name="email">
            </div>
            <div class="form-group">
                <label for="phone">电话:</label>
                <input type="tel" id="phone" name="phone">
            </div>
            <button type="submit" class="btn-register">注册</button>
        </form>
        <div class="link-login">
            已经有账号了？ <a href="login.html">立即登录</a>
        </div>
    </div>

    <script>
        document.getElementById('registrationForm').addEventListener('submit', async function(e) {
            e.preventDefault();

            const studentId = document.getElementById('studentId').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const realName = document.getElementById('realName').value;
            const nickname = document.getElementById('nickname').value;
            const email = document.getElementById('email').value;
            const phone = document.getElementById('phone').value;

            if (password !== confirmPassword) {
                alert('密码和确认密码不匹配！');
                return;
            }

            try {
                const response = await fetch('/api/users/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        studentId: studentId,
                        password: password,
                        realName: realName,
                        nickname: nickname,
                        email: email,
                        phone: phone
                        // userRole can be defaulted in the backend
                    })
                });

                if (response.ok) {
                    alert('注册成功！');
                    // Redirect to homepage after successful registration
                    window.location.href = 'index.html';
                } else {
                    const errorText = await response.text();
                    alert(`注册失败: ${errorText}`);
                    console.error('注册失败:', errorText);
                }
            } catch (error) {
                console.error('注册错误:', error);
                alert('注册失败，请稍后重试');
            }
        });
    </script>
</body>
</html> 